home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 104 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.7 KB

  1. From: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
  2. Message-ID: <9601230947.AA27639@lts.sel.alcatel.de>
  3. X-Original-Date: Tue, 23 Jan 96 10:47:49 +0100
  4. Path: in2.uu.net!bounce-back
  5. Date: 23 Jan 96 09:55:00 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. In-Reply-To: b91926@fsgm01.fnal.gov's message of 23 Jan 96 03:45:36 GMT
  8. Newsgroups: comp.std.c++
  9. Subject: Re: Why no allocator-specific delete?
  10. References: <4dvid8$460@news.bridge.net> <4e0u1s$5fv@engnews1.Eng.Sun.COM> <4e1jfb$1ea@fsgm01.fnal.gov>
  11. Organization: GABI Software, Sarl.
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMQSwg+EDnX0m9pzZAQGbWgF+Ii6LAahMIoG/MBduDN4JvbAAmvmJGD5X
  14.     8KZyFJrL7vDeKwwtVD1BHfoeQwOUrFLB
  15.     =mn+N
  16.  
  17. In article <4e1jfb$1ea@fsgm01.fnal.gov> b91926@fsgm01.fnal.gov (David
  18. Sachs) writes:
  19.  
  20. |> clamage@Eng.Sun.COM (Steve Clamage) writes:
  21.  
  22. |> >In article 460@news.bridge.net, David Byrden <100101.2547@compuserve.com>
  23. |> >writes:
  24.  
  25.     [...]
  26. |> >>but there is no matching version of 'delete'? Are we supposed to use both
  27. |> >>destroy() and deallocate() to get rid of that object?
  28.  
  29. |> >Yes. In the above example:
  30. |> >    p->~T();
  31. |> >    operator delete(p, x);
  32.  
  33. |> How can you guarantee that the first argument to operator delete
  34. |> will be correct? It might be slightly safer to write:
  35.  
  36. |>     void* q = p;  // evaluate before destruction
  37.  
  38. Must be ``void* q = dynamic_cast< void* >( p )''.  Also, of course, T
  39. must have at least one virtual function.  (Not really an additional
  40. constraint; if it didn't have a virtual destructor, the normal case
  41. with non-placement new and delete is undefined.)
  42.  
  43. |>     p->~T();
  44. |>     operator delete(q,x);
  45.  
  46. |> and I am not sure if this is guaranteed to be safe.
  47.  
  48. If T is a class type with a virtual destructor, yes.
  49.  
  50. If T is the actual type of the complete object, and the object is not
  51. an array, yes.
  52.  
  53. If T is an array type, the problem becomes more interesting.  The
  54. correct solution is to not use the built-in array types, and thus
  55. avoid the problem entirely.  (The problem only occurs when memory is
  56. allocated by means of placement new, but freed with a direct call to
  57. operator delete.  In all of the implementations of vector classes I've
  58. seen, the memory is allocated with a direct call to operator new, so
  59. the problem doesn't occur.)
  60.  
  61. --
  62. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  63. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  64. Conseils, itudes et rialisations en logiciel orienti objet --
  65.                 -- A la recherche d'une activiti dans une region francophone
  66. ---
  67. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  68.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  69.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  70.